var oper = getQueryString('oper')
var tenantId = getQueryString('id')
var statusDict = dictByType('status_type')
$(function () {
  $('#oper').val(oper)
  if (oper === 'edit' || oper === 'view') {
    tenantDetail(tenantId)
  } else {
    var result = {
      oper: oper,
      statusDict: statusDict,
      status: '0'
    }
    renderTpl('formTpl', result, 'formBox')
    tplInit()
    dateRange()
    validateForm()
  }
  $.common.initFormInfo();

});

function validateForm(){
  $("#editForm").validate({
    onkeyup: false,
    rules: {
      name: {
        required: true
      },
      code: {
        required: true
      },
      status: {
        required: true
      },
      startTime: {
        required: true
      },
      endTime: {
        required: true
      }
    },
    messages: {
      name: {
        required: "请输入租户名称"
      },
      code: {
        required: "请输入租户编码"
      },
      status: {
        required: "请选择租户状态"
      },
      startTime: {
        required: "请选择开始时间"
      },
      endTime: {
        required: "请选择结束时间"
      }
    },
    focusCleanup: true
  });
}


function tenantDetail(id) {
  $.operate.get(api + '/admin/tenant/' + id, function (res) {
    res.data.oper = oper
    res.data.statusDict = statusDict
    renderTpl('formTpl', res.data, 'formBox')
    tplInit()
    dateRange()
    validateForm()
    $.common.initFormInfo();
    // applyDrawing('#editForm',res.data)
  }, false)
}

function submitHandler() {
  if ($.validate.form()) {
    var dateRange = $('#dateRange').val().split(' 至 ')
    var data = {
      startTime: dateRange[0],
      endTime: dateRange[1],
    }
    $.common.ajaxCommitForm($('#editForm'), false, data);
    return false;
  }
}

function dateRange() {
  layui.use('laydate', function () {
    var laydate = layui.laydate;
    var startDate = laydate.render({
      elem: '#dateRange',
      theme: 'molv',
      trigger: 'click',
      range: '至',
      done: function (value, date) {

      }
    });
  });
}